package com.xdf.xzymanagementsystem.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.xdf.xzymanagementsystem.dao.GreyMainMapper;
import com.xdf.xzymanagementsystem.dao.HalfMainMapper;
import com.xdf.xzymanagementsystem.dao.JcDyeingFactoryMapper;
import com.xdf.xzymanagementsystem.dao.UploadContractMapper;
import com.xdf.xzymanagementsystem.entity.GreyMain;
import com.xdf.xzymanagementsystem.entity.HalfMain;
import com.xdf.xzymanagementsystem.entity.JcDyeingFactory;
import com.xdf.xzymanagementsystem.entity.UploadContract;
import com.xdf.xzymanagementsystem.service.JcDyeingFactoryService;
import com.xdf.xzymanagementsystem.service.SubSystemClient;
import com.xdf.xzymanagementsystem.util.Example;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description : com.xdf.xzymanagementsystem.service.impl(业务层实现类,该类为生成器自动生成).
 * @Author : 张柯
 * @Date : 2020-12-31 14:42:38
 */
@Service
public class JcDyeingFactoryServiceImpl implements JcDyeingFactoryService {
    @Autowired
    private JcDyeingFactoryMapper dao;
    @Autowired
    private UploadContractMapper uploadContractMapper;
    @Autowired
    private SubSystemClient subSystemClient;
    @Autowired
    private GreyMainMapper greyMainMapper;
    @Autowired
    private HalfMainMapper halfMainMapper;

    /**
     * @Describe 新增方法
     * @author 张柯
     * @Date 2020-12-31 14:42:38
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(JcDyeingFactory record) throws Exception {
        return dao.insert(record);
    }

    /**
     * @Describe 选择新增
     * @author 张柯
     * @Date 2020-12-31 14:42:38
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSelective(JcDyeingFactory record) throws Exception {
        record.setCtState(1);
        return dao.insertSelective(record);
    }

    /**
     * @Describe 批量新增
     * @author 张柯
     * @Date 2020-12-31 14:42:38
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int multiInsert(List<JcDyeingFactory> list) throws Exception {
        if (CollectionUtils.isEmpty(list)) return 0;
        return dao.multiInsert(list.stream().map(x -> {
            x.setCtState(1);
            if (x.getHasTax() == null) {
                x.setHasTax(true);
            }
            return x;
        }).collect(Collectors.toList()));
    }

    /**
     * @Describe 修改
     * @author 张柯
     * @Date 2020-12-31 14:42:38
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKey(JcDyeingFactory record) throws Exception {
        return dao.updateByPrimaryKey(record);
    }

    /**
     * @Describe 选择修改
     * @author 张柯
     * @Date 2020-12-31 14:42:38
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKeySelective(JcDyeingFactory record) throws Exception {
        return dao.updateByPrimaryKeySelective(record);
    }

    /**
     * @Describe 修改
     * @author 张柯
     * @Date 2020-12-31 14:42:38
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(JcDyeingFactory record) throws Exception {
        return dao.updateByPrimaryKey(record);
    }

    /**
     * @Describe 批量修改
     * @author 张柯
     * @Date 2020-12-31 14:42:38
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int multiUpdate(List<JcDyeingFactory> list) throws Exception {
        return dao.multiUpdate(list);
    }

    /**
     * @Describe 根据主键查询
     * @author 张柯
     * @Date 2020-12-31 14:42:38
     */
    @Override
    public JcDyeingFactory selectByPrimaryKey(Integer id) throws Exception {
        return dao.selectByPrimaryKey(id);
    }

    /**
     * 根据编号查询
     *
     * @param code
     * @return
     * @throws Exception
     */
    @Override
    public JcDyeingFactory selectByCode(String code) throws Exception {
        if (StringUtils.isEmpty(code)) return null;
        List<JcDyeingFactory> list = dao.selectByParam(JcDyeingFactory.builder().ctCode(code).build());
        return Optional.ofNullable(list).orElse(new ArrayList<>()).stream().findFirst().orElse(null);
    }

    /**
     * @Describe 根据主键删除
     * @author 张柯
     * @Date 2020-12-31 14:42:38
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByPrimaryKey(Integer id) throws Exception {
        GreyMain greyMain = greyMainMapper.selectOneByClientId(id);
        HalfMain halfMain = halfMainMapper.selectOneByClientId(id);
        if (greyMain != null || halfMain != null) throw new RuntimeException("该厂名已被使用不允许删除！！");
        return dao.deleteByPrimaryKey(id);
    }

    /**
     * @Describe 根据Entity查询
     * @author 张柯
     * @Date 2020-12-31 14:42:38
     */
    @Override
    public List<JcDyeingFactory> listQuery(JcDyeingFactory record) throws Exception {
        List<JcDyeingFactory> ret = dao.selectByParam(record);
        List<JcDyeingFactory> ret1 = subSystemClient.filter8SubSystemUserContact(ret,"getId");
        for (JcDyeingFactory jcDyeingFactory : ret1) {
            List<UploadContract> uploadContracts = uploadContractMapper.selectByContactCodeType(jcDyeingFactory.getCtCode(), 0);
            int size = uploadContracts.size();
        }
        return ret1;
    }

    /**
     * 查询
     *
     * @param example
     * @return
     * @throws Exception
     */
    @Override
    public List<JcDyeingFactory> listQuery(Example example) throws Exception {
        return dao.selectByExample(example);
    }

    /**
     * 计数
     *
     * @param example
     * @return
     */
    @Override
    public Long count(Example example) {
        return dao.countByExample(example);
    }

    /**
     * @param id
     * @return
     */
    @Override
    public List<Map<String, Object>> selectSendAddressById(Integer id) {
        JcDyeingFactory record = dao.selectByPrimaryKey(id);
        if (record == null) return null;
        Set<String> set = new HashSet<>();
        set.add(record.getSendAddress01());
        set.add(record.getSendAddress02());
        set.add(record.getSendAddress03());
        set.add(record.getSendAddress04());
        return set.stream().filter(StringUtils::isNotEmpty).map(this::setAndGetMap).collect(Collectors.toList());
    }

    @Override
    public int moveClientSeq(int id, int direction) {
        return dao.moveClientSeq(id, direction);
    }

    private Map<String, Object> setAndGetMap(Object v) {
        Map<String, Object> map = new HashMap<>();
        map.put("value", v);
        return map;
    }
}